package com.agy.filemanager.service.impl;

import com.agy.filemanager.entity.DcBaseMenu;
import com.agy.filemanager.entity.TreeModel;
import com.agy.filemanager.mapper.DcSystemBaseMenuDao;
import com.agy.filemanager.service.DcSystemBaseMenuService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 系统设置-菜单维护
 * @author jpf
 */
@Service
public class DcSystemBaseMenuServiceImpl implements DcSystemBaseMenuService {
    @Autowired
    private DcSystemBaseMenuDao dcSystemBaseMenuDao;

    /**
     * 分页获取菜单信息
     * @param pageNum  当前页码
     * @param pageSize 每页条数
     * @param searchName 查询条件-名称
     * @param menuId 查询条件-菜单ID
     * @return Page 分页的信息
     */
    @Override
    public Page menuInfo(int pageNum, int pageSize,String searchName,String menuId) {
        Page page = PageHelper.startPage(pageNum,pageSize);
        List<DcBaseMenu> list = this.dcSystemBaseMenuDao.menuInfo(searchName,menuId);
        return page;
    }

    /**
     * 获取菜单树
     * @return
     */
    @Override
    public List<TreeModel> menuTreeInfo() {
        List<TreeModel> treeModels = dcSystemBaseMenuDao.menuTreeInfo();
        return treeModels;
    }

    /**
     * 新增菜单
     * @param menu DcBaseMenu实体
     */
    @Override
    public void addMenu(DcBaseMenu menu) {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        menu.setId(uuid);
        List<DcBaseMenu> list = dcSystemBaseMenuDao.getMaxSort();
        DcBaseMenu dcBaseMenu = list.get(0);
        menu.setSort(dcBaseMenu.getSort());
        dcSystemBaseMenuDao.addMenu(menu);
    }

    /**
     * 修改菜单
     * @param menu DcBaseMenu实体
     */
    @Override
    public void editMenu(DcBaseMenu menu) {
        DcBaseMenu menuById = dcSystemBaseMenuDao.getMenuById(menu.getId());
        menu.setFather_id(menuById.getFather_id());
        menu.setLevel(menuById.getLevel());
        menu.setIsyes(menuById.getIsyes());
        menu.setSort(menuById.getSort());
        dcSystemBaseMenuDao.editMenu(menu);
    }

    /**
     * 删除菜单（禁用false 0 启用 true 1）
     * @param menu DcBaseMenu实体
     */
    @Override
    public void disableMenu(DcBaseMenu menu) {
        DcBaseMenu menuById = dcSystemBaseMenuDao.getMenuById(menu.getId());
        menuById.setIsyes(false);
        dcSystemBaseMenuDao.editMenu(menuById);
    }
}
